<?php
header('Content-Type:text/html;charset=utf-8');
require('../../public/zscg.php');
require('../../public/local.php');
$uname=$_POST['uname'];
$nicheng=$_POST['nicheng'];
$e_mail=$_POST['e_mail'];
$pwd1=$_POST['pwd1'];
$pwd=$_POST['pwd'];
$res1=select2($con,'user');
$ctime=time();
$pattern='/\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/';
$arr2=array();
foreach($res1 as $v){
    $arr2 = array_merge($v);
}
if(empty($uname)){
    echo "<script>alert('用户名不能为空');window.location.href='/home/new_user/index.php'</script>";
    die;
}elseif(empty($nicheng)){
    echo "<script>alert('昵称不能为空');window.location.href='/home/new_user/index.php'</script>";
    die;
}elseif(empty($e_mail)){
    echo "<script>alert('邮箱不能为空');window.location.href='/home/new_user/index.php'</script>";
    die;
}elseif(empty($pwd1)){
    echo "<script>alert('密码不能为空');window.location.href='/home/new_user/index.php'</script>";
    die;
}elseif(in_array($uname,$arr2)){
    echo "<script>alert('已存在的用户名');window.location.href='/home/new_user/index.php'</script>";
    die;
}elseif(in_array($nicheng,$arr2)){
    echo "<script>alert('已存在的昵称');window.location.href='/home/new_user/index.php'</script>";
    die;
}elseif(in_array($e_mail,$arr2)){
    echo "<script>alert('已存在的邮箱');window.location.href='/home/new_user/index.php'</script>";
    die;
}elseif(!preg_match($pattern,$e_mail)){
    echo "<script>alert('您输入的邮箱格式不正确');window.location.href='/home/new_user/index.php'</script>";
    die;
}elseif(strcasecmp($_POST['yzm']==$_COOKIE['yzm'])){
    echo "<script>alert('请输入正确的验证码');window.location.href='/home/new_user/index.php'</script>";
    die;
}elseif($pwd1 != $pwd){
    echo "<script>alert('两次输入的密码不相同');window.location.href='/home/new_user/index.php'</script>";
    die;
}
$arr=array('username'=>$uname,'nicheng'=>$nicheng,'e_mail'=>$e_mail,'password'=>md5($pwd),'ctime'=>$ctime);
$res2=insert($con,'user',$arr);
if($res2){
    echo "<script>window.location.href='/home/new_user/insert.php'</script>";
    setcookie('username',$_POST['uname'],0,'');
}else{
    echo "<script>alert('注册失败');window.location.href='/home/new_user/index.php'</script>";
}